package com.travel.server.manager.impl.command.project;

import com.travel.common.CommonConstant;
import com.travel.common.exception.WorkCode;
import com.travel.common.exception.WorkException;
import com.travel.common.util.CollectionUtil;
import com.travel.common.util.CommonUtil;
import com.travel.server.manager.dto.request.project.DeleteProjectGroupRequest;
import com.travel.server.manager.dto.response.EmptyResponse;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.GroupDal;
import com.travel.server.manager.impl.dal.GroupDal.GroupParam;
import com.travel.server.manager.impl.dal.ProjectDal;
import com.travel.server.manager.impl.enums.GroupEnum;
import com.travel.server.manager.model.ProjectGroup;
import com.travel.server.manager.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/10/26 18:07
 * @Desc:
 */
@Component
@Slf4j
public class DeleteProjectGroupCommand extends AbstractCommand<DeleteProjectGroupRequest, EmptyResponse> {

    @Autowired
    private GroupDal groupDal;

    @Autowired
    private ProjectDal projectDal;

    private final static String WAIT_GROUP = "待分组";

    @Override
    protected void checkInput(DeleteProjectGroupRequest request) {
        if(StringUtils.isBlank(request.getIds())){
            throwInvalidParam();
        }
    }

    @Override
    public EmptyResponse handle(DeleteProjectGroupRequest request) {
        checkInput(request);

        List<Integer> idList = CommonUtil.idsToList(request.getIds());
        GroupParam param = new GroupParam();
        param.setIdList(idList);
        List<ProjectGroup> projectGroupList = groupDal.selectByExample(param);

        if(CollectionUtil.isEmpty(projectGroupList)){
            return new EmptyResponse();
        }

        GroupParam waitParam = new GroupParam();
        waitParam.setName(WAIT_GROUP);
        List<ProjectGroup> waitGroupList = groupDal.selectByExample(param);
        ProjectGroup waitGroup = CollectionUtil.getFirst(waitGroupList);
        if(waitGroup == null){
            throw new WorkException(WorkCode.WAIT_GROUP_NOT_EXIST);
        }

        UserInfo userInfo = getUserInfo();
        Date now = new Date();

        for(ProjectGroup projectGroup : projectGroupList) {
            if(projectGroup.getParentClass().equals(GroupEnum.PROJECT.getType())){
                if(groupDal.hasUnCompleteOrder(projectGroup.getName(), null)){
                    throw new WorkException(WorkCode.ORDER_UN_GROUP);
                }
            }else if(projectGroup.getParentClass().equals(GroupEnum.PROJECT_TYPE.getType())){
                if(groupDal.hasUnCompleteOrder(null, projectGroup.getName())){
                    throw new WorkException(WorkCode.ORDER_UN_GROUP);
                }
            }
        }

        for(ProjectGroup projectGroup : projectGroupList){
            projectGroup.setState(CommonConstant.NO);
            projectGroup.setUpdateBy(userInfo.getName());
            projectGroup.setUpdateTime(now);

            if(projectGroup.getParentClass().equals(GroupEnum.PROJECT.getType())){
                projectDal.updateProject(projectGroup.getName(), WAIT_GROUP);
            }else if(projectGroup.getParentClass().equals(GroupEnum.PROJECT_TYPE.getType())){
                projectDal.updateProjectType(projectGroup.getName(), WAIT_GROUP);
            }

            groupDal.updateOrInsert(projectGroup);
        }

        return new EmptyResponse();
    }
}
